package com.mmall.filter;

import com.mmall.common.RequestHolder;
import com.mmall.model.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextImpl;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
public class LoginFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
//        HttpServletResponse resp = (HttpServletResponse) servletResponse;
//
//        SysUser sysUser = (SysUser)req.getSession().getAttribute("user");
//        if (sysUser == null) {
//            String path = "/signin.jsp";
//            resp.sendRedirect(path);
//            ret;
//        }
        RequestHolder.add(req);
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) req.getSession()
                .getAttribute("SPRING_SECURITY_CONTEXT");
        RequestHolder.add((SysUser) securityContextImpl.getAuthentication().getPrincipal());
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }

    public void destroy() {

    }
}
